package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.param.SkuAddNewParam;
import cn.tedu.csmall.product.pojo.vo.SkuListItemVO;
import cn.tedu.csmall.product.service.ISkuService;
import com.example.csmall.commons.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sku")
@Validated
@Api(tags = "10. SKU管理模块")
public class SkuController {

    @Autowired
    private ISkuService skuService;

    // http://localhost:9180/sku/add-new
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/pms/product/add-new')")
    @ApiOperation("新增SKU")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid SkuAddNewParam skuAddNewParam) {
        log.debug("开始处理【新增SKU】的请求，参数：{}", skuAddNewParam);
        skuService.addNew(skuAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/sku/list-by-spu
    @GetMapping("/list-by-spu")
    @PreAuthorize("hasAuthority('/pms/product/read')")
    @ApiOperation("根据SPU查询SKU列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "spuId", value = "SPU ID", required = true, paramType = "query", dataType = "long")
    })
    public JsonResult list(@Range(min = 1, message = "根据相册ID查询图片列表失败，请提供正确的页码值！")Long spuId) {
        log.debug("开始处理【根据SPU查询SKU列表】的请求，SPU：{}", spuId);
        List<SkuListItemVO> skuListItemVOList = skuService.listBySpuId(spuId);
        return JsonResult.ok(skuListItemVOList);
    }
}
